//
//  UIImage+Ext.swift
//  微博
//
//  Created by xm on 2018/3/27.
//  Copyright © 2018年 xm. All rights reserved.
//

import Foundation

extension UIImage{
    
    
    func avatarImage(size: CGSize?,backColor:UIColor=UIColor.white,
                     lineClor:UIColor=UIColor.darkGray) -> UIImage? {
         var size=size
        if size==nil {
            size=self.size
        }
        let rect = CGRect(origin: CGPoint(), size: size!)
        //开启图像上下文
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        
        backColor.setFill()
        UIRectFill(rect)//填充绘制背景色
        
        let path = UIBezierPath(ovalIn: rect)//贝塞尔切园
        path.addClip()//切出来一个圆形
        
        draw(in: rect)
        
        let ovalPath=UIBezierPath(ovalIn: rect)
        ovalPath.lineWidth=2
        lineClor.setStroke()
        ovalPath.stroke()
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        return result
    }
    
}
